package com.lagou.edu.service.impl;

import com.lagou.edu.dao.CodeDao;
import com.lagou.edu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import com.lagou.edu.service.CodeService;

import java.util.Date;
import java.util.Optional;


@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private CodeDao codeDao;

    @Override
    public Date getExpireTimeByEmailAndCode(String email, String code) {
        User user = new User();
        user.setEmail(email);
        user.setCode(code);
        Example<User> example = Example.of(user);
        Optional<User> optional=codeDao.findOne(example) ;
        if(optional.isPresent()){
           return optional.get().getExpiretime();
        }
        return null;
    }

    @Override
    public void saveCodeIntoDatabaseByEmail(User user) {
        codeDao.save(user);
    }
}
